package pwcms;

import org.apache.mahout.cf.taste.pwcms.common.PWCMSRecomender;
import org.apache.mahout.cf.taste.pwcms.common.search.PWCMSSearchOptimizer;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import  org.apache.mahout.cf.taste.recommender.RecommendedItem;
/**
 * Created by IntelliJ IDEA.
 * User: sudharaka
 * Date: Jun 3, 2011
 * Time: 10:32:32 AM
 * To change this template use File | Settings | File Templates.
 */
public class GetRecommendations  extends HttpServlet {

@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
}


protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/*
* Get the value of form parameter
*/
String userid = request.getParameter("userId");

    PWCMSRecomender recomender = new PWCMSRecomender();
   // List<RecommendedItem> list = recomender.getRecommendations(26l,2);
  List<RecommendedItem> list = recomender.getRecommendations(new Integer(userid).longValue(),10); 
  
  request.setAttribute("recommendedItemList", list);
  
  String destination = "/getRecommendationResult.jsp";
	 
	RequestDispatcher rd = getServletContext().getRequestDispatcher(destination);
	rd.forward(request, response);
  
////String welcomeMessage = list.toString() ;
//String welcomeMessage ="Recommendations for User" + userid + "<br/>";
//for(RecommendedItem r : list)
//	welcomeMessage += r.toString() + "<br/>" ; 
//
//// ////////////// search Testing   //////////////
//List<Long> searchResult = new ArrayList<Long>();
//for (int i = 0; i < list.size(); i ++) {
//	searchResult.add(new Long(list.get(i).getItemID()));
//}
//
//
//
//PWCMSSearchOptimizer pwcmsso = new PWCMSSearchOptimizer();
//List<Long> sortedSearchResult = pwcmsso.getPersonalizeSearchResult(new Long(userid), createTestItemIdList());
//
//
//
//welcomeMessage += "<br/>" ; 
//welcomeMessage += "Sorted search result......";
//welcomeMessage += "<br/>" ; 
//
//for(Long r : sortedSearchResult)
//	welcomeMessage += r.toString() + "<br/>" ; 
//
//
///*
///*
//* Set the content type(MIME Type) of the response.
//*/
//response.setContentType("text/html");
//
//PrintWriter out = response.getWriter();
///*
//* Write the HTML to the response
//*/
//out.println("<html>");
//out.println("<head>");
//out.println("<Recommendationss For "+userid+ "</title>");
//out.println("</head>");
//out.println("<body>");
//out.println("<h4>"+welcomeMessage+"</h4>");
//
//out.println("</body>");
//out.println("</html>");
//out.close();
//


}


public void destroy() {

}
List<Long> createTestItemIdList(){
	List<Long> testList = new ArrayList<Long>();
	testList.add(new Long(273));
	testList.add(new Long(2460));
	testList.add(new Long(3519));
	testList.add(new Long(2210));
	testList.add(new Long(3414));
	testList.add(new Long(312));
	testList.add(new Long(3406));
	testList.add(new Long(976));
	testList.add(new Long(3503));
	testList.add(new Long(2816));
	
	return testList;
}
}
